//
//  XDSelectUnitViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDSelectUnitViewController.h"
#import "XDSelectUnitView.h"
#import "XDCouresUnitModel.h"
#import "XDCourseContentListViewController.h"
#import "XDChineseContentListViewController.h"

@interface XDSelectUnitViewController ()
/** 数据源 */
@property(nonatomic,strong) NSArray <XDCouresUnitModel *>* courseUnitModelArr;
@end

@implementation XDSelectUnitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self initNormalRefreshHeaderView];
    //网络请求
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
    
}


-(NSArray<XDCouresUnitModel *> *)courseUnitModelArr{
    
    if (!_courseUnitModelArr) {
        _courseUnitModelArr = [NSMutableArray array];
    }
    return _courseUnitModelArr;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    BOOL isMultipleOfTwo = !(self.courseUnitModelArr.count % 2);
    if (isMultipleOfTwo) {
        return 70 * (self.courseUnitModelArr.count/2) * ScaleHeight;
    }
    
    return  70 + 70 * (self.courseUnitModelArr.count/2) * ScaleHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"123";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //创建视图
    XDSelectUnitView *view =  [[XDSelectUnitView alloc]initWithFrame:self.view.bounds dataArr:self.courseUnitModelArr];
    
    WeakSelf(weakself)
    view.unitClickedBlock = ^(XDCouresUnitModel *unitModel){
        
        if (weakself.type == PracticeTypeChinese || weakself.type == PracticeTypeEnglish ) {  //从汉字练习,单词练习跳转过来的
            
            XDChineseContentListViewController *vc = [[XDChineseContentListViewController alloc]init];
            vc.termId = @(self.termId);
            vc.gradeId = @(self.gradeId);
            vc.unitId = unitModel.ID;
            vc.title = weakself.title;
            [weakself.navigationController pushViewController:vc animated:YES];
            
        }else{  //课时同步跳转过来
            
            XDCourseContentListViewController *vc = [[XDCourseContentListViewController alloc]init];
            vc.unitId = unitModel.ID;
            vc.title = weakself.title;
            [weakself.navigationController pushViewController:vc animated:YES];
        }
    };
    [cell.contentView addSubview:view];
    
    cell.separatorInset = UIEdgeInsetsMake(0, ScreenW, 0, 0);
    return cell;
}


-(void)initNetworkingWithRefreshStatus:(RefreshState)status{

    
    
    
    //汉字练习subjectId = 1;
    //单词练习subjectId = 3;
    if(self.type == PracticeTypeChinese){
        //从汉字练习跳转过来的
        self.subjectId = 1;
    }
    if (self.type == PracticeTypeEnglish) {
        //从单词练习跳转过来的
        self.subjectId = 3;
    }
    
    
    NSString *uid = [AccountTool shareAccountTool].currentAccount.uid;
    NSString *auth = [AccountTool shareAccountTool].currentAccount.authCode;
    
    NSDictionary *dict = @{@"uid":uid,
                           @"gradeid":@(self.gradeId),
                           @"subjectid":@(self.subjectId),
                           };
    
    NSString *url = BaseURL(@"exercise.do?action=getallunitByTid");
    [MBProgressHUD showHUD];
    
    [XDHttpTool get:url authCode:auth params:dict success:^(id responseObj) {
        
        //NSLog(@"%@",responseObj);
        [MBProgressHUD hideHUD];
        
        NSArray *dictArr = responseObj;
        
        if (dictArr.count == 0) {
            [MBProgressHUD showError:XDNetWorkError];
            return ;
        }
        //模型数组
        NSMutableArray *unitModelArr = [XDCouresUnitModel mj_objectArrayWithKeyValuesArray:dictArr];
        
        //服务器没有做学期的分类 需要在网络请求完成后对学期数据分类 学期字段:termType
        
        //上学期
        NSMutableArray *lastTermModelArr = [NSMutableArray array];
        //下学期
        NSMutableArray *nextTermModelArr = [NSMutableArray array];
        
        //1.从数组中取出模型
        for (XDCouresUnitModel *model in unitModelArr) {
            //2.模型中的学期类型分类
            if ([model.termType integerValue] == 1) {  //上学期
                
                [lastTermModelArr addObject:model];
                
            }else if ([model.termType integerValue] == 2){  //下学期
                
                [nextTermModelArr addObject:model];
            }
            
        }
        //3.对比上个界面传入的学期类型
        if(self.termId == 1){
            //上学期
            unitModelArr = lastTermModelArr;
            
        }else{
            //下学期
            unitModelArr = nextTermModelArr;
        }
        
        self.courseUnitModelArr = unitModelArr;
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
        
    }];

}


@end
